﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_Model;
using System.Data;
using V5_Common;
using V5_DAL;

namespace V5_BLL
{
    public class BLLFriend
    {

        DALFriend dal = new DALFriend();
		#region  成员方法

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			return dal.Exists(ID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(ModelFriend model)
		{
			dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public void Update(ModelFriend model)
		{
			dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public void Delete(int ID)
		{
			
			dal.Delete(ID);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ModelFriend GetModel(int ID)
		{
			
			return dal.GetModel(ID);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中。
		/// </summary>
		public ModelFriend GetModelByCache(int ID)
		{
			
			string CacheKey = "ModelFriendModel-" + ID;
			object objModel = DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(ID);
					if (objModel != null)
					{
						int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (ModelFriend)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<ModelFriend> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}

        public List<ModelFriend> GetModelList(int Top,string strWhere, string filedOrder)
        {
            DataSet ds = dal.GetList(Top, strWhere, filedOrder);
            return DataTableToList(ds.Tables[0]);
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<ModelFriend> DataTableToList(DataTable dt)
		{
			List<ModelFriend> modelList = new List<ModelFriend>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				ModelFriend model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new ModelFriend();
					if(dt.Rows[n]["ID"].ToString()!="")
					{
						model.ID=int.Parse(dt.Rows[n]["ID"].ToString());
					}
					model.FriendTitle=dt.Rows[n]["FriendTitle"].ToString();
					model.FriendUrl=dt.Rows[n]["FriendUrl"].ToString();
					model.FriendLogo=dt.Rows[n]["FriendLogo"].ToString();
					model.FriendReadme=dt.Rows[n]["FriendReadme"].ToString();
					if(dt.Rows[n]["IsShow"].ToString()!="")
					{
						model.IsShow=int.Parse(dt.Rows[n]["IsShow"].ToString());
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}


		#endregion  成员方法
        #region 51aspx下载
        #endregion
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere,ref int recordCount)
        {
            recordCount = Input.SetInt(this.GetList(strWhere).Tables[0].Rows.Count);
            return dal.GetList(PageSize, PageIndex, strWhere);
        }
    }
}
